package com.op.teachingliteapp.util;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.op.teachingliteapp.bean.FileBean;
import com.op.teachingliteapp.common.BaseApp;
import com.op.teachingliteapp.common.Common;
import com.op.teachingliteapp.common.api.smartpan.EntityBean;

/**
 * 云盘界面所需用到的方法 2015/8/17 陈宇瀚
 * 
 * @author Administrator
 *
 */
public class SmartPanUtils {

	/**
	 * byte(字节)根据长度转成kb和mb和gb
	 * 
	 * @param bytes
	 * @return
	 */
	public static String convertFileSize(long size) {
		long kb = 1024;
		long mb = kb * 1024;
		long gb = mb * 1024;

		if (size >= gb) {
			return String.format("%.1f GB", (float) size / gb);
		} else if (size >= mb) {
			float f = (float) size / mb;
			return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
		} else if (size >= kb) {
			float f = (float) size / kb;
			return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
		} else {
			return String.format("%d B", size);
		}
	}

	/**
	 * 本地地址转发送地址
	 */
	public static String localConversionSend(String path, Context context) {
		String ownerId = SharedPreferencesUtils.getInstance(context).getString(Common.Prefs.USER_ID, "");
		String pathHeader = Common.rootFilePath.ROOT_FILE_PATH + ownerId;
		String mySmartPanPath = path.replace(pathHeader, "");
		if (mySmartPanPath.equals("")) {
			mySmartPanPath = "/";
		}
		return mySmartPanPath;
	}

	/**
	 * 创建 相应课程的文件夹
	 * 
	 * @param userId
	 * @param list
	 */
	public static void createFolders(FileBean fileBean, Context context) {
		SharedPreferences preferences = SharedPreferencesUtils.getInstance(BaseApp.getAppContext());
		String currentUserId = preferences.getString(Common.Prefs.USER_ID, "");
		DirUtils dirUtils = new DirUtils(context, currentUserId);
		List<String> courses = new ArrayList<String>();
		courses.add(fileBean.getName());
		dirUtils.createDirs(courses, fileBean.getPath().substring(0, fileBean.getPath().lastIndexOf("/")));
	}

	/**
	 * 本地数据与云盘对比
	 * 
	 * @param localList
	 * @param smartPanList
	 * @return
	 */
	public static List<FileBean> localWithSmartPanContrast(List<FileBean> localList, List<EntityBean> smartPanList) {
		for (int i = 0; i < localList.size(); i++) {
			for (int j = 0; j < smartPanList.size(); j++) {
				if (localList.get(i).getName().equals(smartPanList.get(j).getEntityName())) {
					localList.get(i).setCondition(Common.File.HAVA);
					Log.i("SmartPanFragment", "云盘:" + smartPanList.get(j).getEntityName() + "本地:" + localList.get(i).getName());
				}
			}
			Log.i("SmartPanFragment", localList.get(i).getName() + ":" + localList.get(i).getCondition());
			// 如果不等，则说明本地Bean内包含网络上不存在的Bean
			if (localList.get(i).getCondition() == null || !localList.get(i).getCondition().equals(Common.File.HAVA)) {
				localList.get(i).setCondition(Common.File.SMART_NO);
			}
		}
		return localList;
	}

	/**
	 * 云盘与本地数据对比
	 * 
	 * @param localList
	 * @param smartPanList
	 * @param context
	 * @return
	 */
	public static List<FileBean> SmartPanContrastWithlocal(List<FileBean> localList, List<EntityBean> smartPanList, Context context) {
		for (int i = 0; i < smartPanList.size(); i++) {
			for (int j = 0; j < localList.size(); j++) {
				if (smartPanList.get(i).getEntityName().equals(localList.get(j).getName())) {
					smartPanList.get(i).setCondition(Common.File.HAVA);
				}
			}
			// 如果不等，则说明云盘内包含本地不存在的Bean
			if (smartPanList.get(i).getCondition() == null || !smartPanList.get(i).getCondition().equals(Common.File.HAVA)) {
				boolean isFile;
				FileBean addFileBean = new FileBean();
				addFileBean.setCondition(Common.File.THIS_NO);
				addFileBean.setName(smartPanList.get(i).entityName);
				addFileBean.setSize(smartPanList.get(i).getFileSize());
				addFileBean.setIsDown(Common.File.NOT_DOWNLOAD);;
				if (localList.size() >= 1) {
					addFileBean.setPath(localList.get(localList.size() - 1).getPath());
				}
				// 判断是否是文件
				if (smartPanList.get(i).getEntityType().equals(EntityBean.TYPE_FILE)) {
					isFile = true;
				} else {
					isFile = false;
					Log.i("SmartPanFragment", "" + addFileBean.getName());
					SmartPanUtils.createFolders(addFileBean, context);
				}
				addFileBean.setFile(isFile);

				localList.add(addFileBean);
			}
		}
		return localList;
	}

}
